//
//  AFNHttpRequest.m
//  testMIdea
//
//  Created by wendy on 15/8/12.
//  Copyright (c) 2015年 pinksun. All rights reserved.
//

#import "AFNHttpRequest.h"

@implementation AFNHttpRequest
+ (void)afnHttpRequestUrl:(NSString *)url param:(NSDictionary *)param success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    /**** SSL Pinning ****/
#ifdef SSL_Pinning
    NSString *cerPath1 = [[NSBundle mainBundle] pathForResource:@"edge.aia.com" ofType:@"cer"];
    NSData *certData1 = [NSData dataWithContentsOfFile:cerPath1];
    NSString *cerPath2 = [[NSBundle mainBundle] pathForResource:@"IntermediateCA" ofType:@"cer"];
    NSData *certData2 = [NSData dataWithContentsOfFile:cerPath2];
    NSString *cerPath3 = [[NSBundle mainBundle] pathForResource:@"root1" ofType:@"cer"];
    NSData *certData3 = [NSData dataWithContentsOfFile:cerPath3];

    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    [securityPolicy setAllowInvalidCertificates:NO];
    [securityPolicy setPinnedCertificates:@[certData1, certData2, certData3]];

    [manager setSecurityPolicy:securityPolicy];
#endif
    [manager  POST:url
        parameters:param
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               success(responseObject);
           }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               failure(error);
           }];
}

+ (void)afnHttpRequestUrl:(NSString *)url param:(NSDictionary *)param constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
#ifdef SSL_Pinning
    /**** SSL Pinning ****/
    NSString *cerPath1 = [[NSBundle mainBundle] pathForResource:@"edge.aia.com" ofType:@"cer"];
    NSData *certData1 = [NSData dataWithContentsOfFile:cerPath1];
    NSString *cerPath2 = [[NSBundle mainBundle] pathForResource:@"IntermediateCA" ofType:@"cer"];
    NSData *certData2 = [NSData dataWithContentsOfFile:cerPath2];
    NSString *cerPath3 = [[NSBundle mainBundle] pathForResource:@"root1" ofType:@"cer"];
    NSData *certData3 = [NSData dataWithContentsOfFile:cerPath3];

    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    [securityPolicy setAllowInvalidCertificates:NO];
    [securityPolicy setPinnedCertificates:@[certData1, certData2, certData3]];

    [manager setSecurityPolicy:securityPolicy];
#endif
    [manager                 POST:url
                       parameters:param
        constructingBodyWithBlock:^(id < AFMultipartFormData > formData) {
            block(formData);
        }
                          success:^(AFHTTPRequestOperation *operation, id responseObject) {
                              success(responseObject);
                          }
                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                              failure(error);
                          }];
}

@end
